#include "simpengine.h"

/*
	SIMPENGINE.CPP

	The SimpEngine implementation

	Author: Kevin Hawkins
	Date: 4/9/2001
	Description:

*/


void CSimpEngine::OnPrepare()
{
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_SMOOTH);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(54.0f, (float)width/(float)height, 0.1f, 500.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
     
	glLoadIdentity();
}


CSimpEngine::CSimpEngine(const char *szName, bool fscreen, int w, int h, int b) :
		CEngine(szName, fscreen, w, h, b) 
{
	gameCamera = new CCamera;
	gameWorld = new CWorld(gameCamera);
	gameCamera->yaw=45.0;

	bool message = true, show = true;
	Uint32 t1 = SDL_GetTicks(), t2;
	CFont* title = new CFont("fonts/caraway.ttf",40);
	CFont* msg1 = new CFont("fonts/activa.ttf",20);
	CFont* msg2 = new CFont("fonts/activa.ttf",20);
	CFont* msg3 = new CFont("fonts/caraway.ttf",26);

	title->SetRGB(1.0,0.0,0.0);
	title->SetPos3D(-0.25f,0.35f,-0.1f);
	msg1->SetRGB(0.2,0.5,0.9);
	msg2->SetRGB(0.2,0.3,0.9);
	msg3->SetRGB(1.0,1.0,0.0);
	msg3->SetPos3D(-0.35f,-0.40f,-0.1f);
	
	while(message)
	{
		inputSystem->Update();
		if(inputSystem->KeyDown(SDLK_RETURN)) message = false;
		OnPrepare();
		title->Print("MAC For SPEED");
		msg1->SetPos3D(-0.65f,0.15f,-0.1f);
		msg1->Print("Chafa a todos los garfields y completa la vuelta antes que nadie");
		
		msg1->SetPos3D(-0.65f,0.08f,-0.1f);
		msg1->Print("Controles:");
		msg2->SetPos3D(-0.45f,0.03f,-0.1f);
		msg2->Print("w: Avanzar");
		msg2->SetPos3D(-0.45f,-0.02f,-0.1f);
		msg2->Print("s: Retroceder");
		msg2->SetPos3D(-0.45f,-0.07f,-0.1f);
		msg2->Print("a: Movimiento a la izquierda");
		msg2->SetPos3D(-0.45f,-0.12f,-0.1f);
		msg2->Print("d: Movimiento a la derecha");
		msg2->SetPos3D(-0.45f,-0.17f,-0.1f);
		msg2->Print("7,8,9,0: Cambia la vista del vehiculo");
	//	msg2->SetPos3D(-0.45f,-0.22f,-0.1f);
	//	msg2->Print("Raton: Cambiar punto de vista");
	//	msg2->SetPos3D(-0.45f,-0.27f,-0.1f);
	//	msg2->Print("Boton izquierdo de raton: Disparar misil");

		t2 = SDL_GetTicks();
		if(t2 - t1 > 800) 
		{
			show = not show;
			t1 = t2;
		}
		if(show)
			msg3->Print("Pulsa ENTER para comenzar");

		SDL_GL_SwapBuffers();
	}
	delete title;
	delete msg1;
	delete msg2;
}







void CSimpEngine::OnMouseDownL(float x, float y) 
{
//	if (!gameWorld->IsGameDone())
//		gameWorld->player->FireWeapon();
}

void CSimpEngine::OnKeyUp(SDLKey nVirtKey){
	switch(nVirtKey){
		default:
			gameWorld->jugador->stop();
	}
}

void CSimpEngine::OnKeyDown(SDLKey nVirtKey){
	
		switch (nVirtKey){
		    
		    //vista desde arriba
	        case SDLK_0:
                // if (checkpointer)
                //                  gameWorld->jugador->printPosition();
                gameWorld->camera->changeView(0);
	            break;
	            
	        //vista desde atrás
			case SDLK_9:
                // checkpointer = 1; //(checkpointer) ? 0 : 1;
                gameWorld->camera->changeView(1);
				break;
				
			//vista en primera persona
			case SDLK_8:
                gameWorld->camera->changeView(2);
                break;

            //vista hacia atrás
            case SDLK_7:
                gameWorld->camera->changeView(3);
                break;
                
            //palante
	    	case SDLK_UP:
				gameWorld->jugador->moveForward();
				break;
				
			//marcha atrás
			case SDLK_DOWN:
				gameWorld->jugador->moveBackwards();
				break;
				
			//giro derecha
			case SDLK_RIGHT:
				if (inputSystem->KeyDown(SDLK_w))
					gameWorld->jugador->turnRight();
				else if (inputSystem->KeyDown(SDLK_s))
					gameWorld->jugador->backturnRight();
				break;
				
			//giro izquierda
			case SDLK_LEFT:
				if (inputSystem->KeyDown(SDLK_w))
					gameWorld->jugador->turnLeft();
				else if (inputSystem->KeyDown(SDLK_s))
					gameWorld->jugador->backturnLeft();
				break;
				
			//derrape XDDD
            case SDLK_RSHIFT:
                if (inputSystem->KeyDown(SDLK_d))
                    gameWorld->jugador->direction += 1;
				else if (inputSystem->KeyDown(SDLK_a))
					gameWorld->jugador->direction -= 1;
				break;
				
			//salir
			case SDLK_ESCAPE:
				SDL_Event event;
				event.type = SDL_QUIT;
				event.quit.type = SDL_QUIT;
				SDL_PushEvent(&event);
				break;
				
				
				
			case SDLK_PLUS:
				mouseSensitivity += 0.05f;
				break;
			case SDLK_MINUS:
				mouseSensitivity -= 0.05f;
				if (mouseSensitivity < 0.05)
					mouseSensitivity = 0.05f;
				break;
			default:
				break;
		}
}

void CSimpEngine::OnMouseMove(int deltaX, int deltaY)
{
	//if (!gameWorld->IsGameDone())
	//{
        // gameCamera->yaw += deltaX*mouseSensitivity;
        // gameCamera->pitch -= deltaY*mouseSensitivity;
//	}
}

void CSimpEngine::OnMouseMove(int x, int y, int centerX, int centerY)
{
	static float oldX;
	static float oldY;
	static float yaw = 0.0f;
	static float pitch = 0.0f;

	float mX, mY;

	mX = (float)x;
	mY = (float)y;

	if (mX < centerX/2)
		gameCamera->yaw -= 0.25f*mouseSensitivity;
	if (mX > centerX/2)
		gameCamera->yaw += 0.25f*mouseSensitivity;

	if (mY < centerY/2)
		gameCamera->pitch += 0.25f*mouseSensitivity;
	if (mY > centerY/2)
		gameCamera->pitch -= 0.25f*mouseSensitivity;

	oldX = mX;
	oldY = mY;
}
